package com.seekting.mynews.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.seekting.mynews.App;
import com.seekting.mynews.R;
import com.seekting.mynews.connect.tiangou.bean.response.GalleryClass;
import com.seekting.mynews.connect.tiangou.bean.response.TianGouClassify;
import com.seekting.mynews.eventbus.TianGouClassifyEvent;
import com.seekting.mynews.manager.Action;
import com.seekting.mynews.manager.NewsManager;
import com.seekting.mynews.view.OnRecycleItemClickListener;
import com.seekting.mynews.view.TitleAdapter;
import com.seekting.util.SLog;

import java.util.ArrayList;

import cn.trinea.android.common.util.ScreenUtils;
import de.greenrobot.event.EventBus;

public class MyNewsMainActivity extends Activity implements OnRecycleItemClickListener<GalleryClass> {

    private String url = "http://apis.baidu.com/songshuxiansheng/news/news";

    NewsManager mNewsManager;
    private RecyclerView listView;
    private ArrayList<GalleryClass> mTngou;
    private TitleAdapter baseAdapter;
    private int paddingTop;
    private String currentRequest;
    private boolean needRefresh = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        needRefresh = true;
        setContentView(R.layout.activity_main);
        listView = (RecyclerView) findViewById(R.id.classifys);
        mTngou = new ArrayList<GalleryClass>();
        paddingTop = (int) ScreenUtils.dpToPxInt(this, 16);
        baseAdapter = new TitleAdapter(this, mTngou);
        listView.setAdapter(baseAdapter);
        listView.setLayoutManager(new LinearLayoutManager(this));
        baseAdapter.setOnItemClickListener(this);

        EventBus.getDefault().register(this);
        mNewsManager = App.getApp().getManager(NewsManager.class);
        Bundle bundle = new Bundle();
        currentRequest = mNewsManager.request(Action.TianGouClassify, bundle);
    }


    public void onEventMainThread(TianGouClassifyEvent tianGouClassifyEvent) {
        if (TextUtils.isEmpty(currentRequest)) {
            return;
        }
        if (tianGouClassifyEvent != null) {
            TianGouClassify tianGouClassify = tianGouClassifyEvent.getTianGouClassify();
            SLog.d("onEventMainThread " + tianGouClassify);
            if (tianGouClassify != null && currentRequest.equals(tianGouClassify.getUuid())) {
                boolean status = tianGouClassify.isStatus();
                if (status) {
                    mTngou.clear();
                    ArrayList<GalleryClass> tngou = tianGouClassify.getTngou();
                    for (GalleryClass galleryClass : tngou) {
                        mTngou.add(galleryClass);

                    }
                    baseAdapter.notifyDataSetChanged();
                }

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onItemClick(int position, GalleryClass galleryClass) {
        Intent intent = new Intent(this, NewsListActivity.class);
        intent.putExtra("id", galleryClass.getId());
        startActivity(intent);
    }

//    @Override
//    public void onRefresh() {
//        Bundle bundle = new Bundle();
//        bundle.putInt(Request.REQUEST_FROM, Request.FROM_HTTP);
//        currentRequest = mNewsManager.request(Action.TianGouClassify, bundle);
//    }
//
//    @Override
//    public void onAutoRefresh() {
//        Bundle bundle = new Bundle();
//        currentRequest = mNewsManager.request(Action.TianGouClassify, bundle);
//    }
//
//    @Override
//    public void onLoadMore() {
//
//    }
}
